package cn.ffcs.uec.view.util;

public class HexByteUtil
{
  public static byte[] hexStr2ByteArr(String strIn)
    throws Exception
  {
    byte[] arrB = strIn.getBytes();
    int iLen = arrB.length;
    

    byte[] arrOut = new byte[iLen / 2];
    for (int i = 0; i < iLen; i += 2)
    {
      String strTmp = new String(arrB, i, 2);
      arrOut[(i / 2)] = ((byte)Integer.parseInt(strTmp, 16));
    }
    return arrOut;
  }
  
  public static String byteArr2HexStr(byte[] arrB)
    throws Exception
  {
    int iLen = arrB.length;
    
    StringBuffer sb = new StringBuffer(iLen * 2);
    for (int i = 0; i < iLen; i++)
    {
      int intTmp = arrB[i];
      while (intTmp < 0) {
        intTmp += 256;
      }
      if (intTmp < 16) {
        sb.append("0");
      }
      sb.append(Integer.toString(intTmp, 16));
    }
    String result = sb.toString();
    return result;
  }
}

